<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected $_config;
    
    
    function _initConfig()
    {
        $this->_config = new Zend_Config_Ini(APPLICATION_PATH . "/configs/application.ini",APPLICATION_ENV);
        Zend_Registry::set('config',$this->_config);
        Zend_Registry::set('env',APPLICATION_ENV);

        
        
    }
    
    protected function _initDB()
    {
        if ($this->_config->resources->db)
        {
        	$dbAdapter = Zend_Db::factory($this->_config->resources->db);
            $dbAdapter->query("SET NAMES 'utf8'");
			$profiler = new Zend_Db_Profiler_Firebug('All DB Queries');
			$profiler->setEnabled(true);
			 
			// Attach the profiler to your db adapter
			$dbAdapter->setProfiler($profiler);
            
            Zend_Db_Table_Abstract::setDefaultAdapter($dbAdapter);
            Zend_Registry::set('dbAdapter',$dbAdapter);
            
        }
    }
    
    protected function _initConltrols()
    {
        /*require_once 'Zend/Conttroller/Plugin/Acl.php';
        $aclPlugin = new Zend_Controller_Plugin_Acl();
        $aclPlugin->setRoleName();*/
        
        require_once 'Crb/Controller.php';
        require_once 'Crb/Settings.php';
    }

    protected function _initLogger()
    {
        $configuration = Zend_Registry::get('config');
        $logger = $configuration->logger->common;
        if ($logger->enabled)
        {
            $logFileWriter = new Zend_Log_Writer_Stream($logger->file);
            $logger = new Zend_Log($logFileWriter);
            Zend_Registry::set('logger', $logger);
        }
        
        $loggerfb = $configuration->logger->loggerfb;
        
        if ($loggerfb->enabled)
        {
            $writer = new Zend_Log_Writer_Firebug();
            $logger = new Zend_Log($writer);
            Zend_Registry::set('loggerfb',$logger);
        }
        
        $logger = $configuration->logger->error;
        if ($logger->enabled)
        {
            $logFileWriter = new Zend_Log_Writer_Stream($logger->file);
            $logger = new Zend_Log($logFileWriter);
            Zend_Registry::set('logger-error', $logger);
        }
        
        
    }

    
    public function _initRouters()
    {
        $ctrl = Zend_Controller_Front::getInstance();
        $ctrl->setBaseUrl($this->_config->baseurl);
        $router = $ctrl->getRouter();
        //$router = new Zend_Controller_Router_Rewrite();
       /*  $Languages = implode($this->_config->multilanguage->languages->toArray(),'|');
        $locale = Zend_Registry::get('Locale');
      $langRoute = new Zend_Controller_Router_Route(
            '/:lang',
            array('lang'  => 'fr'),
            array('lang'  => '^(' . $Languages . ')$')
        );*/
        
        
        
        
     
        
   $router->addRoute('default', new Zend_Controller_Router_Route(
      ':module/:controller/:action/*',
      array(
        'module'    => 'default',
        'controller'  => 'index',
        'action'    => 'index'
      )
    ));
    
        $router->addConfig($this->_config,'route');
       $compat = new Zend_Controller_Router_Route_Module(array(),
                                                  $ctrl->getDispatcher(),
                                                  $ctrl->getRequest());
                                                  
        
                                                 
$router->addRoute('default', $compat);
        
    }
    
    public function _initRoles()
    {
        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity())
        {
            $User = $auth->getIdentity();
            
            return $User->role;
        }
        else
        {
            return 0;
        }
        
    }
    
    public function _initNavigation()
    {
/*        $this->bootstrapView();
        $view = $this->getResource('view');
        $role = $this->getResource('roles');

        $container = new Zend_Navigation($pages);
        $view->menu = $container;

        return $container;*/
    }

    public function _initRenders()
    { 
             // РќР°СЃС‚СЂРѕР№РєР° СЂР°СЃС€РёСЂРµРЅРёСЏ РјР°РєРµС‚РѕРІ
//             
             // Р—Р°РґР°РЅРёРµ Р±Р°Р·РѕРІРѕРіРѕ URL
             // Р—Р°РґР°РЅРёРµ РїСѓС‚Рё РґР»СЏ view С‡Р°СЃС‚Рё
            // $view->setBasePath($this->_config->views);
             // РЈСЃС‚Р°РЅРѕРІРєР° РѕР±СЉРµРєС‚Р° Zend_View
//             $layout->setView($view);
    }
    
    
    
    public function _initAutoregister()
    {
        require_once 'Zend/Loader/Autoloader.php';
        $autoloader = Zend_Loader_Autoloader::getInstance();
        //$autoloader->registerNamespace('System');
        $autoloader->setFallbackAutoloader(true);
    }
    
    public function _initAcl()
    {
//        $acl = new Acl();
//        $ctrl = Zend_Controller_Front::getInstance();
//        $ctrl->registerPlugin(new Aclplugin($acl));
    }
    
    public function _initPerfomance()
    {
        require_once 'Zend/Loader/PluginLoader.php';
        $classFileIncCache = APPLICATION_PATH . '/../Cache/PluginLoader/Cache.php';
        if (file_exists($classFileIncCache)) include_once $classFileIncCache;
        Zend_Loader_PluginLoader::setIncludeFileCache($classFileIncCache);
    }

   
}

